package com.person.consumer.controller;

import com.person.common.CommonResult;
import com.person.consumer.feignclient.ConsumerFeignClient;
import com.person.consumer.lb.myirule.MyRuleInterface;
import com.person.consumer.pojo.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PaymentController {
    String PAYMENT9001_URL = "http://CLOUDPAYMENTSERVICE";
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
    @Autowired
    private MyRuleInterface myRuleInterface;
    @Autowired
    private ConsumerFeignClient consumerFeignClient;

    @GetMapping("/findone/{id}")
    public CommonResult findOne(@PathVariable("id") int id) {
        CommonResult forObject = restTemplate.getForObject(PAYMENT9001_URL + "/providerpayment9001/payment/find/" + id, CommonResult.class);
        return forObject;
    }

    @PostMapping("/save")
    public CommonResult saveOne(@RequestBody Payment payment) {
        ResponseEntity<CommonResult> commonResultResponseEntity = restTemplate.postForEntity(PAYMENT9001_URL + "providerpayment9001/payment/save", payment, CommonResult.class);
        CommonResult result = commonResultResponseEntity.getBody();
        return result;
    }

    @PostMapping("/update")
    public CommonResult updateByid(@RequestBody Payment payment) {
        ResponseEntity<CommonResult> commonResultResponseEntity = restTemplate.postForEntity(PAYMENT9001_URL + "providerpayment9001/payment/update", payment, CommonResult.class);
        CommonResult commonResult = commonResultResponseEntity.getBody();
        return commonResult;
    }

    @GetMapping("/getinstance")
    public CommonResult getInstance() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println(service);
        }
        List<ServiceInstance> cloudpaymentservice = discoveryClient.getInstances("CLOUDPAYMENTSERVICE");
        for (ServiceInstance serviceInstance : cloudpaymentservice) {
            System.out.println(serviceInstance.getInstanceId());
            System.out.println(serviceInstance.getPort());
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getUri());
        }
        return CommonResult.ok(200, "success", null);
    }

    @RequestMapping("/findone/getentity/{id}")
    public CommonResult getEntity(@PathVariable("id") String id) {
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT9001_URL + "/providerpayment9001/payment/find/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            System.out.println(forEntity.getHeaders() + "\t" + forEntity.getStatusCodeValue());
            return forEntity.getBody();
        } else {
            return CommonResult.ok(400, "操作错误", null);
        }
    }


    @RequestMapping("/find/getentity/{id}")
    public CommonResult getEntityForMyrule(@PathVariable("id") String id){

        ServiceInstance cloudpaymentservice = myRuleInterface.instance(discoveryClient.getInstances("CLOUDPAYMENTSERVICE"));

        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(cloudpaymentservice.getUri() + "/providerpayment9001/payment/find/" + id, CommonResult.class);

        if (forEntity.getStatusCode().is2xxSuccessful()) {
            System.out.println(forEntity.getHeaders() + "\t" + forEntity.getStatusCodeValue());
            return forEntity.getBody();
        } else {
            return CommonResult.ok(400, "操作错误", null);
        }
    }
    @GetMapping("/findbyopenfeign/{id}")
    public CommonResult getOpenFeign(@PathVariable("id") int id){
        return consumerFeignClient.findOne(id);
    }


    @GetMapping("/gettimeout")
    public CommonResult getTimeOut(){
        return consumerFeignClient.getTimeout();
    }

}
